package com.zpkj.portal.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.StringUtil;
import com.zpkj.portal.dto.RequestParameter;
import com.zpkj.portal.dto.ResponseParameter;
import com.zpkj.portal.entity.UserMain;
import com.zpkj.portal.service.user.UserMainService;
import com.zpkj.portal.service.user.UserService;
import com.zpkj.portal.utils.Constant;
import com.zpkj.portal.utils.JsonUtils;
import com.zpkj.portal.utils.SignUtil;
import com.zpkj.portal.utils.ValidateUtils;

/**
 * @author Kelvin 用户
 */
@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserMainService userMainService;

	@Autowired
    private UserService userService;
    
    /**
     * 注册
     */
    @ResponseBody
	@RequestMapping(value = "/systemRegister")
	public String systemRegister(
			HttpServletRequest request,
			@RequestParam(defaultValue="",required=false) String requestSource,
			@RequestParam String sign,
			@RequestParam String jsonParams) throws Exception {
		//请求参数
        RequestParameter params = JsonUtils.fromEncryptJson(jsonParams, RequestParameter.class,requestSource);
        //返回值
        ResponseParameter result = new ResponseParameter("systemRegister",params.getHeader(),requestSource);
        //验证签名
        if(StringUtils.isEmpty(sign) || !SignUtil.checkSign(jsonParams, sign)){
            return result.addError( Constant.ReturnCode.CODE_1999,"签名验证错误").toJson();
        }
        //格式化手机号
        String tel = ValidateUtils.formatMobile(params.getValueFromBody("tel"));
        if(StringUtils.isEmpty(tel)){
            return result.addError(Constant.ReturnCode.CODE_1001,"参数 tel 为空").toJson();
        }
        //非手机号
        if(!ValidateUtils.isMobile(tel)){
        	return result.addError(Constant.ReturnCode.CODE_1002,"参数 tel 格式不正确").toJson();
        }
        //密码
        String pwd = params.getValueFromBody("pwd");
        if(StringUtils.isEmpty(pwd)){
            return result.addError(Constant.ReturnCode.CODE_1001,"参数 pwd 为空").toJson();
        }
        //短信验证码
        String smsCode = params.getValueFromBody("smsCode");
        if( StringUtils.isEmpty(smsCode) ){
        	return result.addError(Constant.ReturnCode.CODE_1001,"参数 smsCode 为空").toJson();
        }
        //邀请码
        String inviteCode = params.getValueFromBody("inviteCode")==null?"":params.getValueFromBody("inviteCode");
        String appType = params.getHeader().getAppDeviceType();
        appType = StringUtil.isEmpty(appType) ? "" : appType;
        return userService.systemRegister(tel,pwd,smsCode,inviteCode.trim(),result,appType);
	}
    
    /**
     * 本系统登录
     */
    @ResponseBody
	@RequestMapping(value = "/systemLogin")
	public String systemLogin(
			HttpServletRequest request,@RequestParam(defaultValue="",required=false) String requestSource,
			@RequestParam String sign,
			@RequestParam String jsonParams) throws Exception {
		//请求参数
        RequestParameter params = JsonUtils.fromEncryptJson(jsonParams, RequestParameter.class,requestSource);
        //返回值
        ResponseParameter result = new ResponseParameter("systemLogin",params.getHeader(),requestSource);
        //验证签名
        if(StringUtils.isEmpty(sign)||!SignUtil.checkSign(jsonParams, sign)){
            return result.addError(Constant.ReturnCode.CODE_1999,"签名验证错误").toJson();
        }
        //格式化手机号
        String tel = ValidateUtils.formatMobile(params.getValueFromBody("tel"));
        if(StringUtils.isEmpty(tel)){
            return result.addError(Constant.ReturnCode.CODE_1001,"参数 tel 为空").toJson();
        }
        //非手机号
        if(!ValidateUtils.isMobile(tel)){
        	return result.addError(Constant.ReturnCode.CODE_1002,"参数 tel 格式不正确").toJson();
        }
        //密码
        String pwd = params.getValueFromBody("pwd");
        if( StringUtils.isEmpty(pwd) ){
        	return result.addError(Constant.ReturnCode.CODE_1001,"参数 pwd 为空").toJson();
        }
        String appType = params.getHeader().getAppDeviceType();
        appType = StringUtil.isEmpty(appType) ? "" : appType;
        return userService.systemLogin(tel,pwd,result,request.getRemoteAddr(),params.getHeader().getMacNo(),appType);
	}
    
    
    /**
     * 修改密码
     */
    @ResponseBody
	@RequestMapping(value = "/updatePwd")
	public String updatePwd(
			HttpServletRequest request,
			@RequestParam(defaultValue="",required=false) String requestSource,
			@RequestParam String sign,
			@RequestParam String jsonParams) throws Exception {
		//请求参数
        RequestParameter params = JsonUtils.fromEncryptJson(jsonParams, RequestParameter.class,requestSource);
        //返回值
        ResponseParameter result = new ResponseParameter("updatePwd",params.getHeader(),requestSource);
        //验证签名
        if(StringUtils.isEmpty(sign) || !SignUtil.checkSign(jsonParams, sign)){
            return result.addError( Constant.ReturnCode.CODE_1999,"签名验证错误").toJson();
        }
        //登录用户id
        String loginUserId = params.getValueFromBody("loginUserId");
        if(StringUtils.isEmpty(loginUserId)){
            return result.addError(Constant.ReturnCode.CODE_1001,"参数 loginUserId 为空").toJson();
        }
        //原密码
        String oldPwd = params.getValueFromBody("oldPwd");
        if(StringUtils.isEmpty(oldPwd)){
            return result.addError(Constant.ReturnCode.CODE_1001,"参数 oldPwd 为空").toJson();
        }
        //新密码
        String newPwd = params.getValueFromBody("newPwd");
        if(StringUtils.isEmpty(newPwd)){
            return result.addError(Constant.ReturnCode.CODE_1001,"参数 newPwd 为空").toJson();
        }
        return userService.updatePwd(loginUserId,oldPwd,newPwd,result);
	}
    
    /**
     * 短信验证码登录
     */
    @ResponseBody
	@RequestMapping(value = "/smsCodeLogin")
	public String smsCodeLogin(
			HttpServletRequest request,
			@RequestParam(defaultValue="",required=false) String requestSource,
			@RequestParam String sign,
			@RequestParam String jsonParams) throws Exception {
		//请求参数
        RequestParameter params = JsonUtils.fromEncryptJson(jsonParams, RequestParameter.class,requestSource);
        //返回值
        ResponseParameter result = new ResponseParameter("smsCodeLogin",params.getHeader(),requestSource);
        //验证签名
        if(StringUtils.isEmpty(sign) || !SignUtil.checkSign(jsonParams, sign)){
            return result.addError(Constant.ReturnCode.CODE_1999,"签名验证错误").toJson();
        }
        //格式化手机号
        String tel = ValidateUtils.formatMobile(params.getValueFromBody("tel"));
        if(StringUtils.isEmpty(tel)){
            return result.addError(Constant.ReturnCode.CODE_1001,"参数 tel 为空").toJson();
        }
        //非手机号
        if(!ValidateUtils.isMobile(tel)){
        	return result.addError(Constant.ReturnCode.CODE_1002,"参数 tel 格式不正确").toJson();
        }
        //验证码
        String smsCode = params.getValueFromBody("smsCode");
        if( StringUtils.isEmpty(smsCode) ){
        	return result.addError(Constant.ReturnCode.CODE_1001,"参数 smsCode 为空").toJson();
        }
        //邀请码
        String inviteCode = params.getValueFromBody("inviteCode")==null?"":params.getValueFromBody("inviteCode");
        String appType = params.getHeader().getAppDeviceType();
        appType = StringUtil.isEmpty(appType) ? "" : appType;
        return userService.smsCodeLogin(tel, smsCode, result, inviteCode, appType);
    }
    
    /**
     * 获取用户简易信息
     */
    @ResponseBody
	@RequestMapping(value = "/getSimpleUserInfo")
	public String getSimpleUserInfo(
			HttpServletRequest request,
			@RequestParam(defaultValue="",required=false) String requestSource,
			@RequestParam String sign,
			@RequestParam String jsonParams) throws Exception {
		//请求参数
        RequestParameter params = JsonUtils.fromEncryptJson(jsonParams, RequestParameter.class,requestSource);
        //返回值
        ResponseParameter result = new ResponseParameter("getSimpleUserInfo",params.getHeader(),requestSource);
        //验证签名
        if(StringUtils.isEmpty(sign) || !SignUtil.checkSign(jsonParams, sign)){
            return result.addError(Constant.ReturnCode.CODE_1999,"签名验证错误").toJson();
        }
        //用户id
        String userId = params.getValueFromBody("userId");
        if(StringUtils.isEmpty(userId)){
            return result.addError(Constant.ReturnCode.CODE_1001,"参数 userId 为空").toJson();
        }
		return result.addData(null).setResultCode(Constant.ReturnCode.CODE_1000).toJson();
	}

    /**
     * 登出
     */
    @ResponseBody
	@RequestMapping(value = "/logout")
	public String logout(
			HttpServletRequest request,
			@RequestParam(defaultValue="",required=false) String requestSource,
			@RequestParam String sign,
			@RequestParam String jsonParams) throws Exception {
		//请求参数
        RequestParameter params = JsonUtils.fromEncryptJson(jsonParams, RequestParameter.class,requestSource);
        //返回值
        ResponseParameter result = new ResponseParameter("logout",params.getHeader(),requestSource);
        //验证签名
        if(StringUtils.isEmpty(sign) || !SignUtil.checkSign(jsonParams, sign)){
            return result.addError(Constant.ReturnCode.CODE_1999,"签名验证错误").toJson();
        }
        //用户id
        String loginUserId = params.getValueFromBody("loginUserId");
        if(StringUtils.isEmpty(loginUserId)){
            return result.addError(Constant.ReturnCode.CODE_1001,"参数 loginUserId 为空").toJson();
        }
        userService.logout(loginUserId);
		return result.setResultCode(Constant.ReturnCode.CODE_1000).toJson();
	}
    
    
    
    /**
     * 用户反馈
     */
    @ResponseBody
	@RequestMapping(value = "/feedback")
	public String feedback(
			HttpServletRequest request,
			@RequestParam(defaultValue="",required=false) String requestSource,
			@RequestParam String sign,
			@RequestParam String jsonParams) throws Exception {
		//请求参数
        RequestParameter params = JsonUtils.fromEncryptJson(jsonParams, RequestParameter.class,requestSource);
        //返回值
        ResponseParameter result = new ResponseParameter("feedback",params.getHeader(),requestSource);
        //验证签名
        if(StringUtils.isEmpty(sign) || !SignUtil.checkSign(jsonParams, sign)){
            return result.addError(Constant.ReturnCode.CODE_1999,"签名验证错误").toJson();
        }
        //用户id
        String loginUserId = params.getValueFromBody("loginUserId");
        if(StringUtils.isEmpty(loginUserId)){
            return result.addError(Constant.ReturnCode.CODE_1001,"参数 loginUserId 为空").toJson();
        }
        //qq
        String qq = params.getValueFromBody("qq");
        if(StringUtils.isEmpty(qq)){
            return result.addError(Constant.ReturnCode.CODE_1001,"参数 qq 为空").toJson();
        }
        //内容
        String content = params.getValueFromBody("content");
        if(StringUtils.isEmpty(content)){
            return result.addError(Constant.ReturnCode.CODE_1001,"参数 content 为空").toJson();
        }
		return result.setResultCode(Constant.ReturnCode.CODE_1000).toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = "/register")
	public String register(HttpServletRequest request,
			@RequestParam(defaultValue = "", required = false) String requestSource, @RequestParam String sign,
			@RequestParam String jsonParams) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			userMainService.addTest();
			map.put("a", 1);
			map.put("b", 2);
			map.put("ret", "success");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("ret", "error");
		}

		return JsonUtils.toJson(map);
	}
	
	@ResponseBody
	@RequestMapping(value = "/getUserInfo")
	public String getUserInfo(HttpServletRequest request,
			@RequestParam(defaultValue = "", required = false) String requestSource, @RequestParam String sign,
			@RequestParam String jsonParams) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			UserMain userMain = userMainService.getUserMainByUserId("cb13c3afd79345e897ff6290deb8bade");
			map.put("ret", "success");
			map.put("userMain", userMain);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("ret", "error");
		}

		return JsonUtils.toJson(map);
	}
	
	@ResponseBody
	@RequestMapping(value = "/getUserInfoById")
	public String getUserInfoById(HttpServletRequest request,
			@RequestParam(defaultValue = "", required = false) String requestSource, @RequestParam String sign,
			@RequestParam String jsonParams) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			UserMain userMain = userMainService.getUserMainById("1");
			map.put("ret", "success");
			map.put("userMain", userMain);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("ret", "error");
		}

		return JsonUtils.toJson(map);
	}

	/**
	 * 接口调用测试
	 */
	@ResponseBody
	@RequestMapping(value = "/inokeTest")
	public String inokeTest(@RequestParam(name = "a") Integer a, @RequestParam(name = "b") Integer b) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("a", a);
		map.put("b", b);
		map.put("ret", "success");
		return JsonUtils.toJson(map);
	}

}
